<template>
	<view class="contont">
		<nav-bar :title="pageInfo.navbarTitle" :iconLeft="true" />
		<uni-forms :model="form" :rules="rules" ref="refForm" label-width="100px">
			<view class="content-bgc">
				<view class="b-con">
					<uni-forms-item label="加工商名称" name="name" required>
						<uni-easyinput v-model="form.name" placeholder="请输入加工商名称" class="iptBor" :inputBorder="false" />
					</uni-forms-item>
					<uni-forms-item label="联系人" name="full_name" required>
						<uni-easyinput v-model="form.full_name" placeholder="请输入联系人" class="iptBor" :inputBorder="false" />
					</uni-forms-item>
					<uni-forms-item label="电话" name="phone" required>
						<uni-easyinput v-model="form.phone" placeholder="请输入电话" class="iptBor" :inputBorder="false" />
					</uni-forms-item>
					<uni-forms-item label="办公地址" name="address">
						<uni-easyinput type="textarea" v-model="form.address" placeholder="请输入办公地址" />
					</uni-forms-item>
					<uni-forms-item label="主营业务" name="business">
						<uni-easyinput type="textarea" v-model="form.business" placeholder="请输入主营业务" />
					</uni-forms-item>
					<uni-forms-item label="备注" name="desc">
						<uni-easyinput type="textarea" v-model="form.desc" placeholder="请输入备注" />
					</uni-forms-item>
					<u-button type="primary" text="提交" @click="save"></u-button>
				</view>
			</view>
		</uni-forms>
	</view>
</template>
<script setup>
import { reactive, ref } from 'vue';
import { onLoad } from '@dcloudio/uni-app'
import enBusinessApi from '../../../api/entrust/en_business'

//页面标题
const pageInfo = reactive({
	navbarTitle: '委外加工入库',
	type: '',
	edit_id: null,
});
onLoad(option => {
	pageInfo.type = option.type
	if (option.type == 'edit') {
		pageInfo.navbarTitle = `${pageInfo.navbarTitle}(修改)`
		get_detail(option.id)
		pageInfo.edit_id = option.id
	} else {
		pageInfo.navbarTitle = `${pageInfo.navbarTitle}(新增)`
	}
})

const refForm = ref(null)
const form = reactive({
	classify: '',
	name: '',
	full_name: '',
	phone: '',
	address: '',
	desc: '',
	business: ''
})

const fields = Object.keys(form);

const rules = {
	name: { rules: [{ required: true, errorMessage: '请输入加工商名称' }] },
	full_name: { rules: [{ required: true, type: 'number', errorMessage: '请输入联系人' }] },
	phone: { rules: [{ required: true, type: 'number', errorMessage: '请输入正确的联系方式', pattern: new RegExp(/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/, 'g') }] },
}

function get_detail(id) {
	enBusinessApi.supplier_detail(id).then(res => {
		const record = uni.$lodash.pick(res, fields)
		Object.assign(form, record)
	})
}

//提交操作
function save() {
	refForm.value.validate().then(() => {
		const params = uni.$lodash.cloneDeep(form) // 深拷贝一份form表单
		params.classify = 2
		if (pageInfo.type == 'add') {
			enBusinessApi.post_supplier(params).then(res => {
				uni.$u.toast('添加成功')
				setTimeout(() => {
					backRefsh();
				}, 1000)
			})
		} else {
			//修改
			enBusinessApi.patch_supplier(params, pageInfo.edit_id).then((res) => {
				uni.$u.toast('修改成功')
				setTimeout(() => {
					backRefsh();
				}, 1000)
			})
		}

	}).catch((error) => {
		uni.$u.toast(error[0].errorMessage);
	})
}

//返回上一级并刷新上一级页面
function backRefsh() {
	uni.navigateBack({
		delta: 1, // 返回的页面层数，1 表示返回上一级
		success: () => {
			uni.$bus.useBusEmit('en_business:refresh')
		}
	})
}
</script>